import { Component, OnInit, ViewChild } from '@angular/core';
import { Http} from '@angular/http';
import { HttpRequest } from "../../../../service/httpRequest";
import { HideElementService } from "../../../../service/hideElement.service";
import { ActivatedRoute } from '@angular/router';
import { PageModel } from "../../../../model/page-model";
import { flyIn } from '../../../../animations/fly-in';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { GlobalVariable } from '../../../../global.config'
import { ValuesPipe } from "../../../../pipe/iterable.pipe";
import { FileLoader } from "../../../../service/fileLoader";
declare var $:any;
@Component({
  selector: 'app-device',
  templateUrl: './device.component.html',
  styleUrls: ['./device.component.css'],
  animations: [
    flyIn
  ]
})
export class DeviceComponent implements OnInit {
  elc = {a:'设备新增',e:'设备编辑',d:'设备删除'};
  @ViewChild('myModal') public myModal:ModalDirective;
  buttonType:number = 0;
  disabled:string = '';
  model = {file:'',name:'',alias_name:'',zdyh:'',sn:'',doc_file:'',xcxlh:'',model:'',mac:'',device_group:'',locate_mode:'',province:'',
    city:'',area:'',street:''};
  selectedId:string = '';

  productId:string = '';

  tool = GlobalVariable.tooltip;
  data:any;
  list = [];
  division:any;
  pageModel = new PageModel();
  page:number = 1;
  constructor(private http:Http,private activeRoute: ActivatedRoute,private fileLoader:FileLoader,private httpRequest:HttpRequest,private he:HideElementService) {
    this.elc = this.he.hideElement(this.elc);
  }

  ngOnInit() {
    this.fileLoader.LoadFileList(['assets/js/uploadify/jquery.uploadify.min.js'], function(){});
    this.activeRoute.params.subscribe(
      params=>{
        this.productId = params['productId'];
        this.getData();
        this.getList();
        this.http.get('assets/diqu.json').subscribe(response =>this.division = response.json());
      }
    );
  }

  getData(){
    this.httpRequest.sendGet('devices/page',{
      product_id: this.productId,
      page:this.page
    }).subscribe(res=>{
      res = this.transData(res);
      this.data = res.result.aaData;
      //console.log(this.data)
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err=>console.log(err));
  }


  getList(){
    this.httpRequest.sendGet('device/group/list2').subscribe(res => {
      this.list = res.result;
    },err=>console.log(err));
  }

  newModal(){
    this.initUploader();
    this.buttonType=1;
    this.model = {file:'',name:'',alias_name:'',doc_file:'',zdyh:'',sn:'',xcxlh:'', model:'',mac:'',device_group:'',locate_mode:'',province:'',
      city:'',area:'',street:''};
    $("#file_uploaded").text('');
    this.myModal.show();
  }


  confirmNew(){
    this.httpRequest.sendPost('devices',{
      product_id: this.productId,
      name: this.model.name,
      alias_name: this.model.alias_name,
      mac: this.model.mac,
      model: this.model.model,
      sn:this.model.sn,
      xcxlh:this.model.xcxlh,
      zdyh:this.model.zdyh,
      doc_file:this.model.doc_file,      
      device_group:this.model.device_group,
      locate_mode: this.model.locate_mode,
      province: this.model.province,
      city: this.model.city,
      area: this.model.area,
      street: this.model.street

    }).subscribe(res=>{
      alert("设备创建成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('设备创建失败!');this.disabled='';console.log(err)});
  }

  editModal(item){
    this.initUploader();
    this.buttonType=2;
    this.selectedId = item.id;
    this.model.name = item.name;
    this.model.alias_name = item.alias_name;
    this.model.model = item.model;
    this.model.mac = item.mac;
    this.model.device_group = '';
    this.model.sn = item.sn;
    this.model.xcxlh = item.xcxlh;
    this.model.zdyh = item.zdyh;
    this.model.doc_file = item.doc_file;
    this.model.province = item.province ? item.province : '';
    this.model.city = item.city ? item.city : '';
    this.model.area = item.area ? item.area : '';
    this.model.street = item.street;
    switch (item.locate_mode){
      case '输入方式':
        this.model.locate_mode = '0';
        break;
      case 'IP':
        this.model.locate_mode = '1';
        break;
      case 'GIS':
        this.model.locate_mode = '2';
        break;
      default:
        this.model.locate_mode = '';
    }
    $("#file_uploaded").text('');   
    this.myModal.show();
  }

  confirmEdit(){
    this.httpRequest.sendPut('devices',{
      id:this.selectedId,
      name: this.model.name,
      alias_name: this.model.alias_name,
      device_group:this.model.device_group,
      locate_mode: this.model.locate_mode,
      province: this.model.province,
      city: this.model.city,
      area: this.model.area,
      street: this.model.street,
      sn:this.model.sn,
      zdyh:this.model.zdyh,
      xcxlh:this.model.xcxlh,
      doc_file:this.model.doc_file,
      model:this.model.model
    }).subscribe(res=>{
      alert("设备修改成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('设备修改失败!');this.disabled='';console.log(err)});
  }

  deleteModal(item){
    this.buttonType=3;
    this.selectedId = item.id;
    this.model.name = item.name;

    this.myModal.show();
  }

  confirmDelete(){
    this.httpRequest.sendDelete('devices',{
      product_id:this.selectedId.split('/')[0],
      mac:this.selectedId.split('/')[1]
    }).subscribe(res=>{
      alert("设备删除成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('设备删除失败!');this.disabled='';console.log(err)});
  }


  transData(data){
    for (var i = 0; i < data.result.aaData.length; i++){

      switch (data.result.aaData[i].locate_mode){
        case '0':
          data.result.aaData[i].locate_mode = '输入方式';
          break;
        case '1':
          data.result.aaData[i].locate_mode = 'IP';
          break;
        case '2':
          data.result.aaData[i].locate_mode = 'GPIS';
          break;
        default:
          break;
      }

      switch (data.result.aaData[i].online){
        case '0' :
          data.result.aaData[i].online = '离线';
          break;
        case '1' :
          data.result.aaData[i].online = '在线';
          break;
        default:
          break;
      }
    }

    return data;
  }

  initUploader() {
    let that =this;
    $('#file_upload').uploadify({
      'swf'      : 'assets/js/uploadify/uploadify.swf',
      'uploader' : "http://101.37.24.73:8080/fileupload/",
      'buttonText': '上传设备手册',
      'fileObjName' : 'file',
      'fileTypeDesc' : '选择文件',
      'fileTypeExts' : '*.doc;*.docx;*.pdf;',
      'method' : 'post',
      'fileSizeLimit':'10MB',
      'auto' : true,
      'multi' : false,
      'onUploadSuccess' : (file,data,response) => {
        let filedata = JSON.parse(data);
        that.model.doc_file = filedata.file_path;
        $("#file_uploaded").text(filedata.file_path+"上传完成");
      }
    });

  }


}
